/* 收件人 */
function updateRecipient() {
    let formData = new FormData()
    let recMobile = document.getElementById('rec-mobile').value

    let re = /\d{11}/
    if (!re.test(recMobile)) {
        alert("请输入11位手机号")
        return
    }

    formData.append('rec-name', document.getElementById('rec-name').value)
    formData.append('rec-mobile', document.getElementById('rec-mobile').value)
    formData.append('rec-address', document.getElementById('rec-address').value)

    fetch('/update/recipient', {
        method: 'POST',
        body: formData
    })
        .then(response => response.json())
        .then(json => {
            if (json.code === 200) {
                alert(json.msg)
            }
        })
}

/* 头像 */
function updateAvatar() {
    const formData = new FormData();
    const fileField = document.querySelector('input[type="file"]');
    console.log(fileField.files[0])

    formData.append("ava-new", fileField.files[0]);
    fetch('/update/avatar', {
        method: 'POST',
        body: formData,
    })
        .then(response => response.json())
        .then(json => {
            if (json.code === 200) {
                alert(json.msg)
            }
        })

}

/* 邮箱 */
function updateEmail() {
    let formData = new FormData()
    formData.append('email-new', document.getElementById('email-new').value)
    formData.append('captcha', document.getElementById('captcha').value)

    fetch('/update/email', {
        method: 'POST',
        body: formData,
    })
        .then(response => response.json())
        .then(json => {
            if (json.code === 200) {
                alert(json.msg)
            }
        })
}

/* 密码 */
function updatePassword() {
    let formData = new FormData()
    let pasOld = document.getElementById('pas-old').value
    let pasNew = document.getElementById('pas-new').value
    let pasConfirm = document.getElementById('pas-confirm').value

    if (pasNew !== pasConfirm) {
        alert("两次输入的密码不一致")
        return
    }

    formData.append('pas-old', pasOld)
    formData.append('pas-new', pasNew)

    fetch('/update/password', {
        method: 'POST',
        body: formData,
    })
        .then(response => response.json())
        .then(json => {
            if (json.code === 200) {
                alert(json.msg)
            }
        })
}

// 默认页面时修改收件人信息
document.getElementById('update').onclick = updateRecipient

function switchPanel(ele) {
    // 切换按钮样式
    document.querySelectorAll('.userInfo .ui-bar div.active')[0].classList.remove('active')
    ele.classList.add('active')

    // 1. 隐藏 active 面板 (删除 active)
    document.querySelectorAll('.userInfo .ui-content div.active')[0].classList.remove('active')

    // 2. 激活面板
    let op = ele.innerHTML
    switch (op) {
        case '收件人':
            document.querySelector('.userInfo .ui-content div#ui-recipient').classList.add('active')
            document.getElementById('update').onclick = updateRecipient
            break
        case '头像':
            document.querySelector('.userInfo .ui-content div#ui-avatar').classList.add('active')
            document.getElementById('update').onclick = updateAvatar
            break
        case '邮箱':
            document.querySelector('.userInfo .ui-content div#ui-email').classList.add('active')
            document.getElementById('update').onclick = updateEmail
            break
        case '密码':
            document.querySelector('.userInfo .ui-content div#ui-password').classList.add('active')
            document.getElementById('update').onclick = updatePassword
            break
    }
}


function getCaptcha() {
    let email = document.getElementById("email-new").value
    let re = /^\w+@[a-z0-9]+\.[a-z]{2,}$/
    if (!re.test(email)) {
        alert("邮箱格式错误")
        return
    }

    let xhr = new XMLHttpRequest()
    xhr.open('GET', '/sendCaptcha?email=' + email)
    xhr.onload = function (e) {
        if (xhr.readyState === 4) {
            if (xhr.status === 200) {
                // 发送成功，不应该使用 alert！！！
                alert("发送验证码成功")
            } else {
                alert("发送验证码失败")
            }
        }
    }
    xhr.onerror = function (e) {
        console.error(xhr.statusText)
    }
    xhr.send(null)
}